package Demo4;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-06-05
 * Time: 19:15
 */
//写一个泛型类，写一个方法，求数组最大值
class Alg<E extends Comparable<E>>{
    public E findMax(E[] array){
        E max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(max) > 0){
                max = array[i];
            }
        }
        return max;
    }
}
class Person implements Comparable<Person>{

    @Override
    public int compareTo(Person o) {
        return 0;
    }
}

class Alg1{
    public <E extends Comparable<E>> E findMax(E[] array){
        E max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(max) > 0){
                max = array[i];
            }
        }
        return max;
    }
}
class Alg2{
    public static <E extends Comparable<E>> E findMax(E[] array){
        E max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(max) > 0){
                max = array[i];
            }
        }
        return max;
    }
}
public class Test {
    public static void main(String[] args) {
        Integer[] integers = new Integer[]{1,2,3,4,5,6,7,8,9,10};
        System.out.println(Alg2.findMax(integers));
    }
    public static void main2(String[] args) {
        Alg1 alg1 = new Alg1();
        Integer[] integers = new Integer[]{1,2,3,4,5,6,7,8,9,10};
        System.out.println(alg1.findMax(integers));//会根据传入的参数类型来推导E类型
        System.out.println(alg1.<Integer>findMax(integers));//完整的写法
    }
    public static void main1(String[] args) {
        Integer[] array = new Integer[]{1,2,3,4,5,6,7,8,9,10};
        Alg<Integer> alg = new Alg<Integer>();
//        alg.findMax(new Person[10]);
        System.out.println(alg.findMax(array));
    }
}
